package com.itbaizhan.ruiji.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itbaizhan.ruiji.common.Result;
import com.itbaizhan.ruiji.pojo.ShoppingCart;
import com.itbaizhan.ruiji.service.ShoppingCartService;
import com.itbaizhan.ruiji.utils.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.swing.plaf.PanelUI;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 购物车 前端控制器
 * </p>
 *
 * @author cc
 * @since 2022-05-30
 */
@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;


    /**
     * 往购物车内部添加
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public Result<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        log.info("购物车数据:{}",shoppingCart.toString());
        //解析一下接受的对象不难发现没有用户ID，所以我们得设置一下用户Id，也就是当前购物车是谁的
        Long userId= BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        Long dishId = shoppingCart.getDishId();
        Long setmealId = shoppingCart.getSetmealId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,userId);
        if (dishId!=null){
            queryWrapper.eq(ShoppingCart::getDishId,dishId);
        }
        if (setmealId!=null){
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart serviceOne = shoppingCartService.getOne(queryWrapper);
        if (serviceOne==null){
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            serviceOne=shoppingCart;
        }
        else {
            Integer number = serviceOne.getNumber();
            serviceOne.setNumber(number+1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.updateById(serviceOne);
        }
        return Result.success(serviceOne);


    }

    @GetMapping("/list")
    public Result<List<ShoppingCart>> list(){
        log.info("查看购物车");
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        queryWrapper.orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
        return Result.success(list);
    }

    @DeleteMapping("/clean")
    public Result<String> clear(){
        log.info("清空购物车");
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        shoppingCartService.remove(queryWrapper);
        return Result.success("清空成功");
    }

//    @PostMapping("/sub")
//    public Result<String> sub(ShoppingCart shoppingCart){
//        log.info("购物车减少");
//        Long dishId = shoppingCart.getDishId();
//        Long setmealId = shoppingCart.getSetmealId();
//        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
//        if (dishId!=null){
//            queryWrapper.eq(ShoppingCart::getDishId,dishId);
//            ShoppingCart serviceOne = shoppingCartService.getOne(queryWrapper);
//            Integer number = serviceOne.getNumber();
//            if (number>1){
//                serviceOne.setNumber(number-1);
//                shoppingCartService.updateById(serviceOne);
//            }
//            if (number==1){
//                shoppingCartService.removeById(serviceOne);
//            }
//        }
//        if (setmealId!=null){
//            queryWrapper.eq(ShoppingCart::getSetmealId,setmealId);
//            ShoppingCart serviceOne = shoppingCartService.getOne(queryWrapper);
//            Integer number = serviceOne.getNumber();
//            if (number>1){
//                serviceOne.setNumber(number-1);
//                shoppingCartService.updateById(serviceOne);
//            }
//            if (number==1){
//                shoppingCartService.removeById(serviceOne);
//            }
//        }
//
//        return Result.success("减少成功");
//    }
@PostMapping("/sub")
public Result<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
    log.info("购物车减少");
    Long dishId = shoppingCart.getDishId();
    Long setmealId = shoppingCart.getSetmealId();
    LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
    if (dishId!=null){
        queryWrapper.eq(ShoppingCart::getDishId,dishId);
    } else if (setmealId!=null){
        queryWrapper.eq(ShoppingCart::getSetmealId,setmealId);
    } else {
        return Result.error("请求参数错误");
    }

    ShoppingCart cart = shoppingCartService.getOne(queryWrapper);
    if (cart == null) {
        return Result.error("购物车中无此商品");
    }

    Integer number = cart.getNumber();
    if (number > 1){
        cart.setNumber(number-1);
        shoppingCartService.updateById(cart);
    } else if (number == 1){
        shoppingCartService.removeById(cart);
    } else {
        return Result.error("商品数量异常");
    }

    return Result.success(cart);
}


}
